package org.tony.utils;

import java.io.File;
import java.net.URI;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.tony.db.DbType;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

/**
 * 触发器模板，采用了freemaker版本
 * @author tony
 *
 */
public class TriggleTemplate {
	
	private static TriggleTemplate instance = new TriggleTemplate();
	
	private boolean init = false ;
	private Configuration cfg ;
	private ConcurrentMap<String, Template> tpls = new ConcurrentHashMap<String, Template>();
	
	private TriggleTemplate()  {
		try {
			init() ;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void init() throws Exception {
		synchronized (this) {
			if(!init) {
				URI uri = Thread.currentThread().getContextClassLoader().getResource("res/triggertpls").toURI();
				cfg = new Configuration();
				cfg.setDirectoryForTemplateLoading(new File(uri)) ;
				cfg.setObjectWrapper(new DefaultObjectWrapper());
				
				tpls.put(DbType.MSSQL2005.name(), cfg.getTemplate(DbType.MSSQL2005.name()+".ftl")) ;
//				tpls.put(DbType.ORACLE.name(), cfg.getTemplate(DbType.ORACLE.name()+".ftl")) ;
			}
		}
	}
	
	public Template getTemplate(String tplName){
		return tpls.get(tplName) ;
	}
	
	public static TriggleTemplate getInstance (){
		return instance ;
	}

}
